<?php

/*
 * Table/PlanningTemplateTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use RHX\Model\Object\PlanningTemplate;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Expression;

class PlanningTemplateTable extends AbstractTableGateway {

    protected $table = 'rh_planning_template';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new PlanningTemplate($this->adapter));
	$this->initialize();
    }

    /**
     * Toute la liste des plannings types
     * 
     * @param boolean $all
     * @param boolean $parent
     * @return ResultSet
     */
    public function getAll($all = false, $parent = true) {
	return $this->select(function(Select $select)use($all, $parent){
	    $where = new Where();
	    if(!$all){
		$where->equalTo('actif', 1);
	    }
	    if(!$parent){
		$where->equalTo('parent', 0);
	    }
	    $select->where($where);
	});
    }

    /**
     * 
     * @param int $siteId
     * @param boolean $all
     * @param boolean $parent
     * @return ResultSet
     */
    public function getAllBySite($siteId, $all = false, $parent = true) {
	return $this->select(function(Select $select)use($siteId, $all, $parent){
	    $where = new Where();
	    $where->equalTo('site_id', $siteId);
	    if(!$all){
		$where->equalTo('actif', 1);
	    }
	    if(!$parent){
		$where->equalTo('parent', 0);
	    }
	    $select->where($where);
	});
    }

    /**
     * Récupérer un planning template
     * 
     * @param int $id
     * @return PlanningTemplate
     * @throws \Exception
     */
    public function get($id) {
	$row = $this->select(array('id' => $id))
		->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver #$id");

	return $row;
    }

    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

    public function save(PlanningTemplate $tpl) {
	$data	 = array(
	    'site_id'	 => $tpl->site_id,
	    'template'	 => $tpl->template,
	    'libelle'	 => $tpl->libelle,
	    'creation'	 => $tpl->creation,
	    'edition'	 => $tpl->edition,
	    'parent' => $tpl->parent,
	    'actif' => $tpl->actif,
	);
	$id	 = (int) $tpl->id;
	if ($id == 0) {
	    $this->insert($data);

	    return $this->lastInsertValue;
	}
	elseif ($this->get($id)) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    throw new \Exception("Impossible de séléctionner #$id");
	}
    }

    public function getTemplatesBySite($site_id, $num) {
	$sql	 = new Sql($this->adapter);
	$select	 = $sql->select();
	$select->from(array('t' => 'rh_employe_template'));

	$select->join(array(
	    'e' => 'rh_employe'
		), 't.matricule = e.matricule', array(
	    'matricule' => 'matricule',
	));

	$select->join(array(
	    's' => 'rh_site'
		), 'e.site_id = s.id', array(
	    'site_libelle' => 'libelle',
	));

	$where = new Where();
	$where->equalTo('s.id', $site_id);
	$where->equalTo('t.template', $num);
	$select->where($where);

	$state		 = $sql->prepareStatementForSqlObject($select);
	$result		 = $state->execute();
	$templates	 = array();

	foreach ($result as $tpl) {
	    $template	 = new \RHX\Model\Object\EmployeTemplate($tpl);
//	    var_dump($tpl);
	    $templates[]	 = $template;
	}

	return $templates;
    }

    /**
     * Récupérer un PlanningTemplate
     *
     * @param Int $template
     * @param Int $site
     * @return PlanningTemplate
     * @throws \Exception
     */
    public function getOne($template, $site) {
	$row = $this->select(function(Select $select) use($template, $site) {
			    $where = new Where();
			    $where->equalTo('template', $template);
			    $where->equalTo('site_id', $site);

			    $select->where($where);
			})->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver la paire(Num : $template, Site : $site)");

	return $row;
    }

    /**
     * Numéro Maximal de Template
     *
     * @return Int
     */
    public function getMaxTemplateNumber() {
	$sql		 = new Sql($this->adapter);
	$select		 = $sql->select();
	$select->from($this->table);
	$select->columns(array(
	    'maxTemplate' => new Expression("MAX(template)"),
	));
	$state		 = $sql->prepareStatementForSqlObject($select);
	$result		 = $state->execute();
	$current	 = $result->current();
	$maxTemplate	 = (int) $current['maxTemplate'];

	return $maxTemplate;
    }

    /**
     * Prochain Numéro de Template
     *
     * @return Int
     */
    public function getNextTemplateNumber() {
	$maxTemplate = $this->getMaxTemplateNumber();

	return++$maxTemplate;
    }

    /**
     * Récupérer un template à partir de son numéro
     *
     * @param type $template
     * @return PlanningTemplate
     */
    public function getTemplate($template) {
	return $this->select(array('template' => $template))
			->current();
    }

}

?>
